Dart has become very popular in recent years. This is because of the release of an SDK called Flutter by Google. Flutter SDK itself is popular because it is very powerful for building multi-platform mobile applications. In developing applications using Flutter, basic knowledge of the Dart programming language is required. Let’s discuss in this tutorial the basics of using the Dart programming language.
It is not difficult to start using the Dart language, especially for those who have already learned programming languages such as JavaScript, PHP, Java, Kotlin, and others.
In syntax, Dart has similarities with other programming languages that have adopted the concept of Object Oriented Programming (OOP).
Dart is an OOP, which was developed by Google and first released on October 10, 2011. Dart is a programming language designed to create applications that are fast on many platforms. When this tutorial was written Dart was implemented in making applications on the mobile platform (Android or iOS), backend (web or server), and desktop (Mac or Windows). Besides being developed to produce fast and multi-platform applications, Dart was also developed specifically to create a beautiful user interface.
To get started running the Dart programming language you need to prepare the following items.
There are three popular code editors commonly used to write Dart programs, they are:
IntelliJ IDEA
Visual Studio Code
Android Studio
Please choose one of the three code editors that is the most familiar for your code writing style.
To simplify the process of writing code and to avoid writing syntax errors, please download the Dart plugin. Please browse to the Extensions menu in the Visual Studio Code editor that you have installed.
Next is downloading the Dart SDK, this SDK contains a library, compiler, transpiler, etc. These elements are the elements needed to start writing the Dart programming language. You can see how to download and install it on the following link. Please adjust the operating system on the computer that you have.
Besides trying to install Dart on your computer you can also try Dart Online Compiler (DartPad). You can use it at the following link.
We will make our first Dart application by following these steps.
Create a folder called dart_tutorial
Create a file called hello.dart
Type the following program code in the hello.dart file
1 2 3
void main() { print('Hello Topcoder!'); }
Navigate the terminal or command line to the dart_tutorial folder.
Run the program that we created by typing the following command in the terminal or command prompt
dart hello.dart
The source code contains the main ()
function, which is the function that will be executed first when the compiler accesses files with the .dart extension.
In starting to learn a type of programming language, variables and data types are things we should not leave behind because they are the elements most often used when writing program syntax. Some data types that can be used on Dart are:
Numbers that include Integers with int keys that are rounded and Double with double keys that have decimal values
Strings with String keys that have text values
Booles with key bools that have true or false values
Lists with key Lists and Maps with key maps that have array values, here is an example of using data types, variables, and values
1
2
3
4
5
6
7
8
9
10
var title = 'Dart Tutorial!';
int prize = 1000;
double weight = 34.33;
String name = 'Robert Downey Junior';
bool filled = true;
List < String > track = ['Design', 'Data Science', 'Development', 'QA'];
Map users = {
'username': 'iqbalhood',
'password': '1234'
};
There are some notes for variables and data types viz. use of the var keyword. This var is a dynamic data type, meaning it can hold any value, so, for example, if you are not sure if the value of A is a String or Integer you can use var. However, if you use dynamic data types this will consume more memory and be more error-prone because the value is not specific.
To try input and output data on command-line based applications the Dart terminal provides the stdin and stdout commands. The following are the steps for using the command:
First import darts: io.
Then use stdout to display the question or the like. This is the same as the normal print function.
Then, stdin to read the input that we input.
1
2
3
4
5
6
7
import 'dart:io';
void main() {
stdout.write('Topcoder Handle: ');
var handle = stdin.readLineSync();
print('Welcome $handle');
}
Like other programming languages, Dart has an operator that can be used to perform operations between two operands (data).
Arithmetic Operators
Addition (+)
Subtraction (-)
Division (/) which produces a double value.
Division (~ /) which produces an integer value.
Multiplication (*)
Remaining for or modulo (%)
Increment (++)
Decrement (-)
Relationship Operators
Same as (==)
Not the same (! =)
Greater (>)
Smaller (<)
Greater than (> =)
Smaller equal to (<=)
Assignment Operator
Filling (=)
Addition (+ =)
Deduction (- =)
Division (/ =)
Multiplication (* =)
Remaining Share (% =)
Logic Operator
And (&&)
Or (||)
Not (!)
Bitwise operator
And (&)
Or (|)
Xor (^)
Not (~)
Left Shift (<<)
Right Shift (>>)
Ternary operator (?)
The following is an example of using some of the operators in the Dart programming language.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import 'dart:io';
main() {
stdout.write("x value: ");
double x = double.parse(stdin.readLineSync());
stdout.write("y value: ");
double y = double.parse(stdin.readLineSync());
double results;
// addition operator
results = x + y;
print("$x + $y = $results");
// reduction operator
results = x - y;
print("$x - $y = $results");
// multiplication operator
results = x * y;
print("$x * $y = $results");
// division operator
results = x / y;
print("$x / $y = $results");
// remainder operator
results = x % y;
print("$x % $y = $results");
}
Branching or decision making is used when making decisions such as, if the value of A is greater than B then run order or another command. In the Dart programming language itself, there are several decision makers that you can use, i.e. else and switch case.
IF
This IF syntax will check if the values in the parentheses are correct then the command will go. As an example here there is the question “15 + 20 =”, then we enter the answer into the result variable. If the result value is equal to 35 then it will print “Correct!”.
1
2
3
4
5
6
7
8
9
10
11
12
import 'dart:io';
void main() {
stdout.write('20 + 15 = ');
int results = int.parse(stdin.readLineSync());
if (results == 35) {
print('Correct!');
}
}
\
IF ELSE
IF ELSE is the same as IF, but where else is functioning when the value of “IF” is wrong, run the command that exists in else. This example is the same as before but if the result value is not 35 then it prints “Wrong”.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import 'dart:io';
void main() {
stdout.write('20 + 15 = ');
int results = int.parse(stdin.readLineSync());
if (results == 35) {
print('Correct!');
} else {
print('Wrong!');
}
}
ELSE IF
This is the same as IF and ELSE, only ELSE IF it is more tiered in its decision-making experiments. At this time we will try the logic of pronunciation of Good Morning, Good Day and Good Night. We will take the input of the hour value; if the hour value is less than ten then the message “Good Morning!” will be displayed, but if the hour value is less than twenty then the message “Good Day!” will be displayed. If the point value is not from one of the above it will display the message "Good Night!”.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import 'dart:io';
void main() {
stdout.write('Hour = ');
int hour = int.parse(stdin.readLineSync());
if (hour < 10) {
print('Good Morning!');
} else if (hour < 20) {
print('Good Day!');
} else {
print('Good Night!');
}
}
SWITCH
This structure is similar to IF ELSE only for this case switch uses a parallel system if the IF ELSE structure uses a serial system, and branch execution is performed sequentially starting from the top structure. In the switch case structure, the execution is carried out directly in parallel.
As an example, check the month value. If the value is the same as the month value in the available case, the corresponding month will be printed. After this break case N should have another one, the default that works if the value is not in the existing case, then run the existing command at the default.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import 'dart:io';
void main() {
stdout.write('Insert Month in Numbers: ');
int month = int.parse(stdin.readLineSync());
switch (month) {
case 1:
print("January");
break;
case 2:
print("February");
break;
case 3:
print("March");
break;
case 4:
print("April");
break;
case 5:
print("May");
break;
case 6:
print("June");
break;
case 7:
print("July");
break;
case 8:
print("August");
break;
case 9:
print("September");
break;
case 10:
print("October");
break;
case 11:
print("November");
break;
case 12:
print("December");
break;
default:
print("Incorrect month format");
}
}
In addition to using the structure above, we can also make branches by using the ternary operator (?). This logging is actually another form of IF / ELSE.
1
2
3
4
5
6
7
8
9
10
11
import 'dart:io';
main() {
stdout.write("EXAMINATION SCORE? ");
int answer = int.parse(stdin.readLineSync());
// Use the ternary operator instead of if / else
var results = (answer > 60) ? "PASSED" : "FAILED";
print(results);
}
Iteration is used when we will execute a command or function repeatedly according to the number of iterations that we want. In the Dart programming language, there are several types of iterations that you can use such as for, for in, while, and do-while.
FOR
For is a loop that corresponds to a predetermined value.
1
2
3
4
5
6
7
8
9
10
import 'dart:io';
main() {
stdout.write("Number of Loops: ");
int iteration = int.parse(stdin.readLineSync());
for (int i = 1; i <= iteration; i++) {
print("Loop -$i");
}
}
WHILE
This type of loop will execute a looping command if the conditions (conditions) are met. The following is an example code of this type of loop.
1
2
3
4
5
6
7
8
9
10
11
12
import 'dart:io';
main() {
stdout.write("Number of Loops: ");
int iteration = int.parse(stdin.readLineSync());
int i = 0;
while (i < iteration) {
print("While Loop - $i");
i++;
}
}
DO - WHILE
DO-WHILE is a type of loop that will do the looping command first, then check the condition or the occurrence of the loop. If the conditions are met, then DO-WHILE will continue looping. Conversely, the looping process will stop (break).
1
2
3
4
5
6
7
8
9
10
11
12
13
import 'dart:io';
main() {
stdout.write("Number of Loops: ");
int iteration = int.parse(stdin.readLineSync());
int i = 0;
do {
print("do While - $i");
i++;
}
while (i < iteration);
}
FOR - IN
This type of loop is used to value the items in a list and repeat until the items in the list run out. As an example, here is a list of month names that contain three values; we make for in retrieve the values in the list, then print the value until it runs out, which will print January, February and March.
1 2 3 4 5 6 7
void main() { List < String > months = ['January', 'February', 'March']; for (var month in months) { print(month); } }
Dart programming language is included in the languages that apply the concept of OOP. In practice, the use of functions in Dart has the same form and structure as the Java programming language, PHP and so on.
Like other programming languages, Dart has a void function. Void means this function does not return a return value. In the declaration, the void can use the void keyword at the beginning of the function or without using the void keyword.
1
2
3
void first_function() {
print('January');
}
Next is the function that returns the return value, as in the example here, the function starts with the String data type, which means the return value of the function is also a String.
1 2 3
String second_function() { return 'Februrary'; }
Then there are functions that have parameters that can include variables when the function is called.
1 2 3 4
String third_function(String name) { String call = 'Hello' + name; return call; }
In Dart there is a way to present a function in a more concise way, often referred to as the Arrow Function / Lambda Function. This method of summarizing is usually used when the function to be summarized has only one value.
1 2 3 4 5 6 7
// Regular Function String Users(String handle) { return handle; } // Lambda / Arrow Function String Users(String handle) => handle;
Just like in other OOP based programming languages, class is used as a place to be filled with several variables and methods that can later be used in other program listings by making a part of the class an object.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Animal {
String name;
var habitat;
String getName() {
return this.name;
}
void setName(String name) {
this.name = name;
}
String getHabitat() {
return this.habitat;
}
void setHabitat(String habitat) {
this.habitat = habitat;
}
}
main() {
var peacock = new Animal();
peacock.setName("Jhon");
peacock.setHabitat("Water");
...print
}
The constructor is a special function of the class that is responsible for initializing class variables. The constructor has the exact name of the class name. The constructor can or may not have parameters. The constructor does not return a value.
1
2
3
4
5
6
7
8
9
void main() {
Handle('iqbalhood');
}
class Handle {
Handle(String username) {
print(username);
}
}
This discussion is a quick step to start learning the Dart programming language so you can speed up to the next step to create real world applications, such as mobile apps, using the Flutter SDK. To find out more about Dart you can learn it through the official documentation of the Dart programming language.